#include "mainscence.h"
#include "ui_mainscence.h"

#include <QAction>
#include <QPainter>
#include <QDebug>

mainScence::mainScence(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::mainScence){
    ui->setupUi(this);

    //设置固定大小
    this->setFixedSize(320, 588);

    this->setWindowIcon(QIcon(":/resource/Coin0001.png"));

    this->setWindowTitle("翻金币");

    //菜单栏退出按钮
    ui->actionexit->setIcon(QIcon(":/resource/Coin0001.png"));
    connect(ui->actionexit, &QAction::triggered, [=](){
        this->close();
    });


//    开始界面 和选择关卡界面
    startpane = new startWidget();
    levlpane = new ChooseLeaveWidget();

    connect(startpane, &startWidget::sendGotoLevlPane, [=](){
        qDebug() << "去关卡界面";
        this->setWindowTitle("选择关卡");
        this->setCentralWidget(levlpane);
    });

    connect(levlpane, &ChooseLeaveWidget::sendBackToStartpane, [=](){
        qDebug() << "去开始界面";
        this->setWindowTitle("翻金币");
        this->setCentralWidget(startpane);
    });

    //处理选择关卡后的信号
    connect(levlpane, &ChooseLeaveWidget::sendGotoGamepane, this, &mainScence::gotoGamepaneSlot);

    this->setCentralWidget(startpane);

}
//跳转到游戏界面槽函数 如果用lamda表达式 可以这样写 [=](GameWidget * gw){}
void mainScence::gotoGamepaneSlot(GameWidget * gw){
    this->setWindowTitle(QString("游戏中：第 %1 关").arg(gw->num));
    this->setCentralWidget(gw);

    connect(gw, &GameWidget::sendBackToSelectpane, [=](){
        qDebug() << "从游戏界面返回关卡界面";
        this->setWindowTitle("选择关卡");
        this->setCentralWidget(levlpane);
        delete gw;
//        gw = NULL;
    });

}

//setCentralWidget方法会将当前控件删除，所以再次调用原来的控件会崩溃，重写方法释放控件
void mainScence::setCentralWidget(QWidget * wg){
    QWidget * curWidget = this->centralWidget();
    if(curWidget){
        curWidget->setParent(NULL);
    }
     QMainWindow::setCentralWidget(wg);
}



mainScence::~mainScence(){
    delete ui;
}

